<?php



if(get('notid')==''){
    // need to choose a subject to edit

    $nots = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_notifications ORDER BY subscription_id ASC");
   
    foreach($nots as $k=>$v){
        $nots[$k]['sub_data'] = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_subscriptions WHERE id = '{$v['subscription_id']}'");
        $nots[$k]['editUrl'] = $Antz->addParamsToUrl($actionUrls['edit_notification'], array('notid'=>$v['id']));
    }
    
    $this->smarty->assign('nots', $nots);
    
    echo $this->ProcessTemplate('admin.edit-notification.select.tpl');
   
}else{


  
  $notid = (int) get('notid');
  
  if($DB->countRows(cms_db_prefix().'isubscribe_notifications', "id = '{$notid}'")==0){
      Antz::addError($this->Lang('invalid_notification_id'));
      Antz::redir($actionUrls['edit_notification']);
  };




  
  if(Antz_IntelliForm::submitted('edit_notification')){
      $error = false;
      
      $subid = (int) post('subscription_id');
      $numDays = (int) post('numDays');
      $subject = post('subject');
      $content = post('content');
      
      if($DB->countRows(cms_db_prefix().'isubscribe_subscriptions', "id = '{$subid}'")==0){
        $error = true;
        Antz::addFormError('subscription_id');
      }
      
      if($numDays < 0){
          $error = true;
          Antz::addFormError('numDays');
      }
      
      if(trim($subject)==''){
          $error = true;
          Antz::addFormError('subject');
      }
      
      if(trim($content)==''){
          $error = true;
          Antz::addFormError('content');
          Antz::addError($this->Lang('content_is_blank'));
      }
      
      if(!$error){
          $dbParams = array(
              'subscription_id' => $subid,
              'num_days' => $numDays,
              'subject' => $subject,
              'content' => $content
          );
          $DB->update(cms_db_prefix().'isubscribe_notifications', $dbParams, "id = '{$notid}'");
          Antz::addSuccess($this->Lang('notification_updated'));
          Antz::redir($actionUrls['edit_notification']);
      }
  
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  $notData = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_notifications WHERE id = '{$notid}'");
  
  
  $subOpts = array();
  $subs = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_subscriptions ORDER BY is_deleted, is_active, title");
  foreach($subs as $k=>$v){
      $subOpts[$v['id']] = $v['title'].' ['.$v['keyname'].']';
  };
  
  $formFields = array(
      array(
          'name' => 'subscription_id',
          'type' => 'select',
          'value' => post('subscription_id', $notData['subscription_id']),
          'label' => $this->Lang('subscription'),
          'options' => $subOpts
      ),
      array(
          'name' => 'subject',
          'type' => 'text',
          'value' => post('subject', $notData['subject']),
          'label' => $this->Lang('subject'),
          'class' => 'wide'
      ),
      array(
          'name' => 'content',
          'type' => 'tinymce',
          'value' => post('content', $notData['content']),
          'label' => $this->Lang('content'),
          'id' => 'IS_new-notification'
      ),
      array(
          'name' => 'numDays',
          'type' => 'text',
          'value' => post('numDays', $notData['num_days']),
          'label' => $this->Lang('days_prior'),
          'class' => 'short'
      )
  );
  
  $form = Antz::makeRapidForm($formFields, $this->Lang('edit_notification_legend'), 'edit_notification');
  
  $form->addSubmit($this->Lang('submit'));
  
  echo '<h1>'.$this->Lang('edit_notification').'</h1>';
  
  echo $form->render();



}
